<?php
namespace Admin\Controller;
use Think\Controller;
//工具类
use Admin\Common\Tools as T;
//逻辑类
use Admin\Common\Logic;

use Admin\Common\Api\UserApi;
class NavController extends BaseController {
private $HAND_LOGINC   = null;


 protected function _initialize(){
   parent::_initialize();
    $this->HAND_LOGINC = new Logic();

 }
public   function index(){
    //获取到菜单信息
    $this->display();

   }

//获取菜单
 public  function menu(){
      $title =I('request.title','','trim');
    if(!empty($title)){
       $where['name'] =array('like',"%{$title}%");
       // print_r($_where);exit;
       // $_where['parcelname']=array('like',"%{$keywords}%");
    }
    $where['parent_id'] = I("request.parent_id") == ''?0:I("request.parent_id");
    $_where_order_field["listrows"] =I("request._listrows");
    $_where_order_field["where"] = $where;
    $_where_order_field["field"] = '*';
    $_where_order_field["order"] = 'sort,id ASC';
    $_datalist= $this->_getListsFromModel('Menu',$_where_order_field);
        // print_r($_datalist['page_html']);exit;
    $this->assign('parent_id',$where['parent_id'] );
    $this->assign('title',$title );
    $this->assign('lists',$_datalist);
    $this->display('menu');

  }
    //获取上一级的父级id
  public  function  return_up(){
      $parent_id=I("request.parent_id");
      if(!empty($parent_id)&&is_numeric($parent_id)&&$parent_id>0){
          $authRuleEntity= M("Menu")->where(array("closed"=>1,"id"=>$parent_id))->find();
         $return_up_id= $authRuleEntity["parent_id"];
          $this->ajaxReturn(array("status"=>1,"info"=>$return_up_id));
      }else{
          $this->ajaxReturn(array("status"=>0,"info"=>"亲，已经没有页面可以返回了！"));
      }
  }



  public  function  add_menu(){
         $is_add=intval(I("request.is_add"));
          $this->assign("is_add",$is_add);
          if($is_add==0){
              $this->auth_rule_add_ok();
          }else if($is_add==1){
              $this->auth_rule_update();
          }
  }

    /**
     * 无限级分类
     * @access public
     * @param Array $data     //数据库里获取的结果集
     * @param Int $pid
     * @param Int $count       //第几级分类
     * @return Array $treeList
     */
     public static function tree(&$data,$pid = 0,$count = 1,&$treeList=array()) {
        foreach ($data as $key => $value){
            if($value['parent_id']==$pid){
                $value['count'] = $count;
                $treeList []=$value;
                self::tree($data,$value['id'],$count+1,$treeList);
            }
        }

    }
   /**
     * 添加
     */
    public  function  auth_rule_add_ok(){
        if(IS_POST){
            $data["name"]=I("request.name");
            $data["name_url"]=I("request.name_url");
            $data["closed"]=I("request.closed");
            $data["parent_id"]=I("request.parent_id");
            $data["sort"]=I("request.sort");
            $data["is_menu"]=I("request.is_menu");
            $data["add_time"]=date('Y-m-d H:i:s');
      
            $auth_ruleModel=M("Menu");
            $rowid=$auth_ruleModel->data($data)->add();
            if($rowid){
                $exe_scriptstr = "<script>

                    setTimeout(function(){
                    var win=    art.dialog.opener;
                                win.load();
                                art.dialog.close();
                    },1000);
           </script>";

                $this->success("添加成功！".$exe_scriptstr);
            }else{
                $this->error("添加失败！");
            }
        }else{
            $auth_ruleModel= M("Menu");
            $authDataList=$auth_ruleModel->where(array("closed"=>1))->order("sort desc")->select();
            $treeList=array();
            self::tree($authDataList,0,1,$treeList);
            $parent_id= I("request.parent_id",0);
            $this->assign("parent_id",$parent_id);
            $this->assign("parentlist",$treeList);
            $this->display('auth_rule_add');
        }
    }

    /**
     * 修改
     */
    public  function  auth_rule_update(){
        if(IS_POST){
            $id=I("request.id");
            $data["name"]=I("request.name");
            $data["name_url"]=I("request.name_url");
            $data["closed"]=I("request.closed");
            $data["sort"]=I("request.sort");
            $data["is_menu"]=I("request.is_menu");
            $parent_id=I("request.parent_id");
            if(!empty($parent_id)&&is_numeric($parent_id)){
                $data["parent_id"]=$parent_id;
            }
            $auth_ruleModel=M("menu");
            $rowid=$auth_ruleModel->where(array("id"=>$id))->save($data);
            if($rowid>0||$rowid===0){
                $exe_scriptstr = "<script>
                    setTimeout(function(){
                    var win=    art.dialog.opener;
                                win.load();
                                art.dialog.close();
                    },1000);
                    </script>";

                $this->success("修改成功！" . $exe_scriptstr);
            }else{
                $this->success("修改失败！");
            }
        }else{

            $auth_ruleModel= M("Menu");
            $authDataList=$auth_ruleModel->where(array("closed"=>1))->order("sort desc")->select();
            $treeList=array();
            self::tree($authDataList,0,1,$treeList);
      
            $parent_id= I("request.parent_id",0);
            $this->assign("parent_id",$parent_id);
            $this->assign("parentlist",$treeList);
            $id=I("request.id");
            $authEntity=$auth_ruleModel->where(array("id"=>$id))->find();
            $this->assign("m",$authEntity);
            $this->assign("is_add",I("request.is_add"));
            $this->assign("status",$authEntity['closed']);
            $this->assign("is_display",$authEntity['is_menu']);
            $this->display('auth_rule_add');
        }

    }
  public  function delete_ok(){
        $id=I("request.id");
        $auth_ruleModel=M("Menu");
        $auth_ruleModel->where(array("parent_id"=>array("like","%$id%"),"closed"=>1))->delete();
        $auth_ruleModel->where(array("id"=>$id,"closed"=>1))->delete();
        // $this->success("删除成功！");
         $this->ajaxReturn(array('status'=>1,'info'=>'删除成功！'));
  }

  public  function delete_admin_ok(){
        $id=I("request.id");
        $auth_ruleModel=M("Admin");
        $auth_ruleModel->where(array("id"=>$id))->delete();
        // $this->success("删除成功！");
        $this->ajaxReturn(array('status'=>1,'info'=>'删除成功！'));
  }
//权限分配
  public function access_control(){
    $id = I("request.id");
    $this->assign('id',$id);
    $this->display();
  }
//修改权限
  public function save_power(){
    $id = I("request.id",'','trim');
    $rules = I("request.rules");
    if($id<=0){
      $this->ajaxReturn(array('state'=>0,'msg'=>'参数传递错误'));
    }
    if(empty($rules)){
      $this->ajaxReturn(array('state'=>0,'msg'=>'总要来点权限吧'));
    }
    $rulesArr = explode(',', $rules);
    sort($rulesArr);
    $rulesStr = implode(',', $rulesArr);
    $res = D('admin')->where('id = '.$id)->setField('power',$rulesStr);
    if($res){
      $this->ajaxReturn(array('state'=>1,'msg'=>'权限修改成功'));
    }else{
      $this->ajaxReturn(array('state'=>0,'msg'=>'没有修改权限'));
    }
  }


  public function menu_ajax(){
   
     $id= I("request.id");
     //获取到该管理员的权限
     $auth_ruleModel= M("Menu");
     $authDataList=$auth_ruleModel->field('id,name,name_url,parent_id')->where(array("closed"=>1))->order("sort,id ASC")->select();
     $where['id'] = $id;
     if($id>0){
      $admin_list =D('Admin')->where('id = '.$id)->field('power')->find();
          if($admin_list['power'] == 'all'){
       //超级管理员
        foreach($authDataList as $key=>$value){
             $authDataList[$key]["pId"]=$value["parent_id"];
             $authDataList[$key]["pid"]=$value["parent_id"];
             $authDataList[$key]["checked"]= 1;
        }
            $this->ajaxReturn($authDataList);
        }else{
           $rulesArray=explode(",",$admin_list['power']);
            foreach($authDataList as $key=>$value){
                $authDataList[$key]["pId"]=$value["parent_id"];
                $authDataList[$key]["checked"]=in_array($value["id"],$rulesArray);
            }
                $this->ajaxReturn($authDataList);
        }
     }else{
        
        foreach($authDataList as $key=>$value){
             $authDataList[$key]["pId"]=$value["parent_id"];
             $authDataList[$key]["pid"]=$value["parent_id"];
        }
            $this->ajaxReturn($authDataList);
    
     }
    //获取该用户的权限
    


   // print_r($treeList);exit;


  }

//管理员信息
  public function edit_admin(){
    $is_add=intval(I("request.is_add"));
          $this->assign("is_add",$is_add);
      if($is_add==0){
          $this->upd_admin();
      }else if($is_add==1){
          $this->add_admin();
      }
   
  }

  //添加管理员
public function add_admin(){

         if(IS_POST){
            $data["name"]=I("request.name");
            $data["pwd"]=sha1(I("request.pwd"));
            $data["power"]=I("request.rules");
            $data["sort"]=I("request.sort");
            $data["is_activity"]=I("request.status");
            $data["add_time"]=date('Y-m-d H:i:s');
            $auth_ruleModel=M("Admin");
            $rowid=$auth_ruleModel->data($data)->add();
            if($rowid){
              $this->ajaxReturn(array('state'=>1,'msg'=>'添加成功'));
            }else{
                $this->ajaxReturn(array('state'=>0,'msg'=>'添加失败'));
            }

        }else{
             $this->assign("status",1);
            $this->display('add_admin');
        }

}
//修改管理员
public function upd_admin(){
 if(IS_POST){
            $id=I("request.id");
            $data["name"]=I("request.name");
            $data["pwd"]=sha1(I("request.pwd"));
            $data["power"]=I("request.rules");
            $data["sort"]=I("request.sort");
            $data["is_activity"]=I("request.status");
            $auth_ruleModel=M("Admin");
            $rowid=$auth_ruleModel->where(array("id"=>$id))->save($data);
            if($rowid>0||$rowid===0){
                $this->ajaxReturn(array('state'=>1,'msg'=>'修改成功'));
            }else{
               $this->ajaxReturn(array('state'=>0,'msg'=>'修改失败'));
            }
        }else{
/*            $auth_ruleModel= M("Menu");
            $authDataList=$auth_ruleModel->where(array("closed"=>1))->order("sort desc")->select();
            $treeList=array();
            self::tree($authDataList,0,1,$treeList);
            print_r($treeList);exit;
            $parent_id= I("request.parent_id",0);
            $this->assign("parent_id",$parent_id);
            $this->assign("parentlist",$treeList);*/
            $id=I("request.id");
            $authEntity=D('Admin')->where(array("id"=>$id))->find();
            $this->assign("m",$authEntity);
            $this->assign("is_add",I("request.is_add"));
            $this->assign("status",$authEntity['closed']);
            $this->assign("id",$id);
            $this->assign("is_display",$authEntity['is_menu']);

            $this->display('add_admin');
        }

}




//获取菜单列表
 public  function nav_menu(){
   $model = D("menu");
   $map =  array('parent_id'=>0,'closed'=>1);
   $data = $this->_list($model,$map);
   $json_data = json_encode($data);
   $menu_data = str_replace('iconcls','iconCls', $json_menu);
   echo $json_data;

  }

  public function nav_son_menu(){
    $id= empty($_REQUEST['id'])?"1":$_REQUEST['id'];
    $model = D("menu");

    $map =  array('parent_id'=>$id,'closed'=>1);
    $data = $this->_list($model,$map);
       $json_data = json_encode($data);
         $menu_data = str_replace('iconcls','iconCls', $json_menu);
     echo $json_data;

  }



  // public function add_menu(){
  //   $res = array('success'=>0,'msg'=>'数据传输错误');
  //   $arrdata = empty($_POST)?'':$_POST;
  //   $id = empty($_GET['id'])?"":$_GET['id'];
  //   if($id == ""){
  //     $this->ajaxReturn($res);exit;
  //   }
  //   if($arrdata ==""){
  //      $res = array('success'=>0,'msg'=>'请输入对应内容');
  //      $this->ajaxReturn($res);
  //   }
  //     $result = $this->HAND_LOGINC->add_menu($arrdata,$id);
  //    $this->ajaxReturn($result);

  // }

}
